from hep.aida import IFunction
from hep.aida import IHistogram1D
from java.awt import Color
from java.awt import Font
from java.util import Locale
from java.io import File
from jhplot.jadraw import JaText
from jhplot.jadraw import JaAxes
from jhplot  import H1D
from jhplot  import F1D
from jhplot  import HPlotJa
from jscinttools.utilities.jhplotutilities import HPlotJaUtil
from jscinttools.utilities.aidautilities import AidaFileReader

Locale.setDefault(Locale.ENGLISH)

""" Define if Showing or Saving the Plots """
showFigures = False
writeFigures = not showFigures


""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/BGO/QFin"
aidaFile = "ScintillationAnalysis_BGO"
outPath = "/home/marc-antoine/Documents/Dropbox/BGO"

temper = "3"
plotVersion = "1"
xRange = [0, 250]


"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
afr = AidaFileReader(aidaPath, aidaFile)
objPath = "/Spectra"
specName = "Spectrum_" + temper + ".0K"
funcName = "Spec_Func_" + temper + ".0K"
outFile = "BGOSpec" + temper + "K_V" + plotVersion

aidaSpec = afr.grabHisto1D(objPath + File.separator + specName)
normFac = aidaSpec.maxBinHeight()
aidaSpec.scale(1.0 / normFac)
spec = H1D(aidaSpec)

aidaFunc = afr.grabFunction(objPath + File.separator + funcName)
aidaFunc.setParameter("Amp_moy", aidaFunc.parameter("Amp_moy") / normFac)
aidaFunc.setParameter("Amp_gauss", aidaFunc.parameter("Amp_gauss") / normFac)
func = F1D(aidaFunc)

label = JaText("T = " + temper + " K", 0.2, 0.25, "NDC")


"""
###############################################################
# Define Graphical Attributs
###############################################################
"""
""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 20)

""" Spectrum graphical attributs """
spec.setTitle(temper + " K")
spec.setStyle("h")
spec.setColor(colors[0])
spec.setPenWidth(2)
spec.setErrAll(False)

""" Function graphical attributs"""
func.setTitle(temper + " K")
func.setMin(xRange[0])
func.setMax(xRange[1])
func.setPoints(100)
func.setColor(colors[1])
func.setPenWidth(2)

""" Label graphical attributs"""
label.setFont(legendFont)
label.setColor(colors[0])


"""
###############################################################
# Create the Plotter
###############################################################
"""
plotter = HPlotJa("Spectra", 600, 600, 1, 1, True)
plotter.setGTitle("Spectrum of 511 keV &gamma; in BGO", titleFont)
plotter.setAntiAlias(False)
plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
plotter.setRange(xRange[0], xRange[1], 0, 1.1)
plotter.setLogScale(0, False)
plotter.setLogScale(1, False)
plotter.setTicFont(ticFont)
plotter.setShowStatBox(False)
plotter.showKey(False)
plotter.setNameX("Number of Photons")
plotter.setNameY("Normalized Counts")


""" Plots all the JHep objects"""
plotter.draw(spec)
plotter.draw(func)
plotter.draw(label)


""" Modifies the JaAxes and the axes labels"""
theAxes = plotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setTicksSize(0, theAxes.getTicksSize(0))
theAxes.setSubTicksSize(0, theAxes.getSubTicksSize(0))
theAxes.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes)

labX = plotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, plotter)
labY = plotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, plotter, -0.005)

plotter.update()

""" Show or save the plot"""
if showFigures:
	plotter.visible(True)
else:
	plotter.visible(False)
	for i in range(len(figType)):
		plotter.export(outPath + File.separator + outFile + figType[i])
		print("Writting File : " + outPath + File.separator + outFile + figType[i])
		
plotter.update()
print("That's all folks !")
